# 自动取款机的存取款模拟系统
# 支持用户注册、登录、查询、取款、存款服务

# 1.用户信息列表
account_list = [
    {"account": "666666", "password": "123456", "fund" : 1000 },
    {"account": "888888", "password": "654321", "fund" : 2000 },
    {"account": "999999", "password": "153246", "fund" : 3000 }
]

# 2.登录账户
def login(account,password):
    # 判断账号、密码是否正确
    for acc in account_list:
        if acc["account"] == account and acc["password"] == password:
            #print("登录成功！")
            return True
        if acc["account"] == account and acc["password"] != password:
            print("密码错误！")
            return False
    else:
        print("用户不存在！")
        return "注册"

# 3. 查询账号信息
def show_account_list(root_acc):
    # 设置信息呈现格式
    print('|{0: <8}|{1: <8}|{2: <6}|'.format('id', "account", "password", "fund"))
    print('-' * 30)
    # 打印当前用户信息
    for acc in account_list:
        if acc["account"] == root_acc:
            print('|{0: <8}|{1: <8}|{2: <6}|'.format(acc["account"], acc["password"], acc["fund"]))

# 4. 取钱
def expend(root_acc, money):
    # 判断当前账户余额是否充足，是则允许操作，否则提示“余额不足”
    for acc in account_list:
        if acc["account"] == root_acc:
            if acc["fund"] >=  money:
                acc["fund"] = acc["fund"] - money
                break
    else:
        print("余额不足！")

# 5. 存钱
def save_money(root_acc, money):
    for acc in account_list:
        if acc["account"] == root_acc:
            acc["fund"] +=  money

# 6. 用户注册
def register():
    # 提示新用户输入相关信息
    root_acc = input("请输入账号:")
    root_password = input("请输入密码:")
    root_fund = 0
    # 将新用户信息添加到用户列表中
    account_list.append(dict(account = root_acc, password = root_password,fund = root_fund))
    print("账号创建成功！")

# 7.退出程序
def quit():
    print("="*12 + "祝您生活愉快！" + "="*12)
    exit()



# 设置用户交互界面
print("="*12 + "取存款模拟系统" + "="*12 )
while True:
    # 设置用户交互界面，并提示用户输入相关信息
    print('-' * 38)
    print("|" + " " * 13 + "请登录账号" + " " * 13 + "|")
    print('-' * 38)
    root_acc = input("请输入账号：")
    password = input("请输入密码：")

    # 若账号密码正确，则开启各项业务服务
    while login(root_acc,password) == True:
        # 设置业务服务窗口
        print('\n' + "="*14 + " 相关操作 " + "="*14 )
        print('{0:<1}{1:<8}{2:8}{3:<8}{4:8}'.format(" ", "1. 查询", "2. 取款", "3. 存款", "4. 退出"))
        print("=" *38)
        # 判断用户输入指令并进行相关操作
        opera = input("请选择相应操作：")
        if opera == "1":
            show_account_list(root_acc)
        elif opera == "2":
            money = int(input("请输入取款金额："))
            expend(root_acc, money)
        elif opera == "3":
            money = int(input("请输入存款金额："))
            save_money(root_acc,money)
        elif opera == "4":
            quit()

    # 若账号不存在，开启“注册、退出”业务服务
    while login(root_acc, password) == "注册":
        # 设置注册界面
        print("=" * 14 + " 相关操作 " + "=" * 14)
        print('{0:<9}{1:<12}{2:12}'.format(" ", "1. 注册", "2. 退出" ))
        print("=" * 38)
        # 判断用户输入指令并进行相关操作
        opera = input("请选择相应操作：")
        if opera == "1":
            register()
            break
        elif opera == "2":
            quit()